package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.FilterActivity;
import com.chuanyang.driverh.mainhome.adapter.MoneyFollowDriverAdapter;
import com.chuanyang.driverh.response2.MoneyFollowDriverEntity;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.response2.ResultMoneyFollow;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class MoneyFollowDriverFragment extends BaseListFragment {
    private MoneyFollowDriverAdapter listAdapter;
    private View title_bar;
    private RadioGroup rg_top;
    private RadioButton rb0,rb1,rb2,rb3;
    private RequestFilter filter;

    public void filter(){
        Intent intent=new Intent(activityContext, FilterActivity.class);
        intent.putExtra("type","4");
        intent.putExtra("data",filter);
        startActivity(intent);
    }

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new MoneyFollowDriverAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_header_money_follow_no_click, null);
        rg_top=(RadioGroup) title_bar.findViewById(R.id.rg_top);
        rb0=(RadioButton) title_bar.findViewById(R.id.rb0);
        rb1=(RadioButton) title_bar.findViewById(R.id.rb1);
        rb2=(RadioButton) title_bar.findViewById(R.id.rb2);
        rb3=(RadioButton) title_bar.findViewById(R.id.rb3);

        if (root.getChildCount()>0)root.removeAllViews();
        root.addView(title_bar,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData() {
        rb0.setText("全部应收(元)"+"\n"+"0");
        rb1.setText("当月应收(元)"+"\n"+"0");
        rb2.setText("当月已收(元)"+"\n"+"0");
        rb3.setText("累计未收(元)"+"\n"+"0");
        filter=new RequestFilter();
        onRefresh() ;
    }



    @Override
    protected void initListener() {
//        rb0.setOnClickListener(this);
//        rb1.setOnClickListener(this);
//        rb2.setOnClickListener(this);
//        rb3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rb0:
                break;
            case R.id.rb1:
                break;
            case R.id.rb2:
                break;
            case R.id.rb3:
                break;
        }
    }

    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())){
            params.put("fleetId", SharedPrefUtil.getFleetId());
        }

        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        params.put("driverType", StringUtil.isBlank(SharedPrefUtil.getFleetId())?1:2);

        if (!StringUtil.isBlank(filter.getStartPoint())){
            params.put("beginCity",filter.getStartPoint());
        }
        if (!StringUtil.isBlank(filter.getEndPoint())){
            params.put("endCity",filter.getEndPoint());
        }
        if (!StringUtil.isBlank(filter.getStartTime())){
            params.put("orderTimes",filter.getStartTime());
        }
        if (!StringUtil.isBlank(filter.getEndTime())){
            params.put("orderTimee",filter.getEndTime());
        }
        if (!StringUtil.isBlank(filter.getSelectType())){
            params.put("selectType",filter.getSelectType());
        }

        if (filter.getOrderStatus2()!=null && filter.getOrderStatus2().size()==1){
            params.put("defrayStatus",filter.getOrderStatus2().get(0));
        }
        if (filter.getOrderType2()!=null &&filter.getOrderType2().size()>0){
//            params.put("orderType",filter.getOrderType2());
            params.put("orderType",StringUtil.getSplitString(filter.getOrderType2()));
        }

        MyHttpUtil.moneyFollow(getActivity(),params, new MyCallback<ResultMoneyFollow>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ResultMoneyFollow result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        setHeader(result.getData().getMonthInfo());
                        setEntity(result.getData().getData().getData());

                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    private void setEntity(List<ResultMoneyFollow.OutData.MiddleData.InnderData> data) {
      List<MoneyFollowDriverEntity> entities=new ArrayList<MoneyFollowDriverEntity>();
        try {
            if (data!=null && data.size()>0) {
                for (int i = 0; i < data.size(); i++) {
                    for (int j = 0; j < data.get(i).getWaybills().size(); j++) {
                        MoneyFollowDriverEntity entity = new MoneyFollowDriverEntity();
                        if (j == 0) {
                            entity.setType(1);
                        } else {
                            entity.setType(2);
                        }
                        entity.setOrderId(data.get(i).getOrderId());
                        entity.setPayStatus(data.get(i).getPayStatus());
                        entity.setFleetId(data.get(i).getFleetId());
                        entity.setDefrayStatus(data.get(i).getDefrayStatus());
                        entity.setCurrency(data.get(i).getCurrency());

                        entity.setOrderNo(data.get(i).getOrderNo());
                        entity.setOrderTime(data.get(i).getOrderTime());
                        entity.setBeginCity(data.get(i).getBeginCity());
                        entity.setEndCity(data.get(i).getEndCity());
                        entity.setOrderType(data.get(i).getOrderType());
//                        entity.setGoodsNames(data.get(i).getGoodsNames());
                        //以下为运单的。
                        entity.setGoodsNames(data.get(i).getWaybills().get(j).getGoodsName());
                        entity.setOriginalWeight(data.get(i).getWaybills().get(j).getOriginalWeight());
                        entity.setOriginalNum(data.get(i).getWaybills().get(j).getOriginalNum());
                        entity.setSumGoodsNum(data.get(i).getWaybills().get(j).getSumGoodsNum());
                        entity.setSumGoodsWeight(data.get(i).getWaybills().get(j).getSumGoodsWeight());
                        entity.setSumPrice(data.get(i).getWaybills().get(j).getSumPrice());
                        entity.setWaybillNo(data.get(i).getWaybills().get(j).getWaybillNo());
                        entity.setWaybillId(data.get(i).getWaybills().get(j).getWaybillId());

                        entities.add(entity);


                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        setDataResult(entities);
    }

    private void setHeader(ResultMoneyFollow.OutData.MonthInfoBean info) {
        rb0.setText("全部应收(元)"+"\n"+StringUtil.getWeight(info.getAllBuFrom()+""));
        rb1.setText("当月应收(元)"+"\n"+StringUtil.getWeight(info.getWhenMonthBuFrom()+""));
        rb2.setText("当月已收(元)"+"\n"+StringUtil.getWeight(info.getWhenMonthThen()+""));
        rb3.setText("累计未收(元)"+"\n"+StringUtil.getWeight(info.getAddNotBuFrom()+""));
    }

    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    public void onEventMainThread(FilterEvent event) {
        if ("4".equals(event.getType()) ){
            filter=event.getData();
            onRefresh();
        }
    }
}
